package com.example.pt.repository;

import com.example.pt.entity.Notification;
import com.example.pt.entity.Peer;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface NotificationRepository extends JpaRepository<Notification, Long> {

    @Query("SELECT n FROM Notification n WHERE n.user_id = :user_id")
    List<Notification> findByUserId(@Param("user_id") Long user_id);

    //状态标记
    @Modifying
    @Transactional
    @Query("UPDATE Notification n SET n.status = :status WHERE n.id = :id")
    int updateStatus(@Param("id") Long id, @Param("status") int status);

    @Modifying
    @Transactional
    @Query(value = "INSERT INTO notification(user_id, title, content, created_at) " +
            "VALUES (:user_id, :title, :content, :created_at)",
            nativeQuery = true)
    void insertNotification(
            @Param("user_id") Long user_id,
            @Param("title") String title,
            @Param("content") String content,
            @Param("created_at") LocalDateTime created_at
    );


}
